package ytdp.careagle.com.ytdp.myview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;

import java.util.ArrayList;

import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.entity.ModelRelationship;


/**
 * Created by lida on 2017/7/30 0030.
 */

public class DecorationCarType extends RecyclerView.ItemDecoration {
    private final Paint mPaint;
    private final Rect mBounds;
    private final int mTitleHeight;
    private final int mTitleFontSize;
    private final int mLineHeight;
    private Context mContext;
    private ArrayList<ModelRelationship.ChildrenBean> list = new ArrayList<>();
    private static int COLOR_TITLE_BG = Color.parseColor("#f1f1f1");
    private static int COLOR_TITLE_FONT;
    private static int COLOR_TITLE_LINE = Color.parseColor("#f1f1f1");
    private final int dp5;

    public DecorationCarType(Context context) {
        this.mContext = context;
        COLOR_TITLE_FONT = context.getResources().getColor(R.color.text_title);
        dp5 = (int) context.getResources().getDimension(R.dimen.dp5);
        mPaint = new Paint();
        mBounds = new Rect();
        mTitleHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, context.getResources().getDisplayMetrics());
        mLineHeight = (int) mContext.getResources().getDimension(R.dimen.dp1);
        mTitleFontSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18, context.getResources().getDisplayMetrics());
        mPaint.setTextSize(mTitleFontSize);
        mPaint.setAntiAlias(true);
    }

    public void setDatas(ArrayList<ModelRelationship.ChildrenBean> list) {
        this.list.clear();
        this.list.addAll(list);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            int position = params.getViewLayoutPosition();
            //我记得Rv的item position在重置时可能为-1.保险点判断一下吧
            if (position > -1) {
                if (position == 0) {//等于0肯定要有title的
                    drawTitleArea(c, left, right, child, params, position);
                } else {//其他的通过判断
                    if (null != list.get(position).getModelYear() && !list.get(position).getModelYear().equals(list.get(position - 1).getModelYear())) {
                        //不为空 且跟前一个tag不一样了，说明是新的分类，也要title
                        drawTitleArea(c, left, right, child, params, position);
                    } else {
                        //none
                        drawLineArea(c, left, right, child, params, position);
                    }
                }
            }
        }
    }

    //如果是头部的就画个头  不是头部的话就画线
    private void drawLineArea(Canvas c, int left, int right, View child, RecyclerView.LayoutParams params, int position) {
        mPaint.setColor(COLOR_TITLE_LINE);
        c.drawRect(left + dp5 * 2, child.getTop() - params.topMargin + 2, right, child.getTop() - params.topMargin, mPaint);
    }

    //    悬停控件  也可以自己画黑线和调整左右间距
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {//最后调用 绘制在最上层
        int pos = ((LinearLayoutManager) (parent.getLayoutManager())).findFirstVisibleItemPosition();

        String tag = list.get(pos).getModelYear() + "款";
        //View child = parent.getChildAt(pos);
        View child = parent.findViewHolderForLayoutPosition(pos).itemView;//出现一个奇怪的bug，有时候child为空，所以将 child = parent.getChildAt(i)。-》 parent.findViewHolderForLayoutPosition(pos).itemView
        mPaint.setColor(COLOR_TITLE_BG);
        c.drawRect(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getRight() - parent.getPaddingRight(), parent.getPaddingTop() + mTitleHeight, mPaint);
        mPaint.setColor(COLOR_TITLE_FONT);
        mPaint.getTextBounds(tag, 0, tag.length(), mBounds);
        c.drawText(tag, child.getPaddingLeft() + dp5 * 2,
                parent.getPaddingTop() + mTitleHeight - (mTitleHeight / 2 - mBounds.height() / 2),
                mPaint);
    }

    //具体绘制
    private void drawTitleArea(Canvas c, int left, int right, View child, RecyclerView.LayoutParams params, int position) {
        mPaint.setColor(COLOR_TITLE_BG);
        c.drawRect(left, child.getTop() - params.topMargin - mTitleHeight, right, child.getTop() - params.topMargin, mPaint);
//        TODO  上面的宽黑色
//        mPaint.setColor(COLOR_TITLE_LINE);
//        c.drawRect(left, child.getTop() - params.topMargin - mTitleHeight, right, child.getTop() - params.topMargin - mTitleHeight - dp5, mPaint);
        //TODO 下面的线
        mPaint.setColor(COLOR_TITLE_LINE);
        c.drawRect(left, child.getTop() - params.topMargin + 2, right, child.getTop() - params.topMargin, mPaint);
        mPaint.setColor(COLOR_TITLE_FONT);
/*
        Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
        int baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;*/

        mPaint.getTextBounds(list.get(position).getModelYear(), 0, list.get(position).getModelYear().length(), mBounds);
        c.drawText(list.get(position).getModelYear() + "款", child.getPaddingLeft() + dp5 * 2, child.getTop() - params.topMargin - (mTitleHeight / 2 - mBounds.height() / 2), mPaint);
    }

    //确定整个布局的宽高
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //画横线，就是往下偏移一个分割线的高度
        int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        //我记得Rv的item position在重置时可能为-1.保险点判断一下吧
        if (position > -1) {
            if (position == 0) {//等于0肯定要有title的
                //5dp为了留出黑色的宽度
                outRect.set(0, mTitleHeight, 0, 0);
            } else {//其他的通过判断
                if (null != list.get(position).getModelYear() && !list.get(position).getModelYear().equals(list.get(position - 1).getModelYear())) {
                    outRect.set(0, mTitleHeight, 0, 0);//不为空 且跟前一个tag不一样了，说明是新的分类，也要title
                }
//                else {
//                    outRect.set(0, mLineHeight, 0, 0);
//                }
            }
        }
    }
}
